package com.jms.boot.listener.chanin;

import com.jms.framework.event.ApplicationCloseEvent;
import com.jms.framework.listener.ApplicationListener;
import com.jms.framework.listener.ListenerChain;

import java.util.ArrayList;

public class ApplicationCloseChain implements ListenerChain {

    private final ArrayList<ApplicationListener<ApplicationCloseEvent>> listeners;
    private final ApplicationCloseEvent event;

    private static volatile ApplicationCloseChain instance;

    private ApplicationCloseChain(ArrayList<ApplicationListener<ApplicationCloseEvent>> listeners, ApplicationCloseEvent event) {
        this.listeners = listeners;
        this.event = event;
    }

    public static ApplicationCloseChain getInstance(ArrayList<ApplicationListener<ApplicationCloseEvent>> listeners, ApplicationCloseEvent event) {
        if (instance == null) {
            synchronized (ApplicationCloseChain.class) {
                if (instance == null) {
                    instance = new ApplicationCloseChain(listeners, event);
                }
            }
        }
        return instance;
    }
    @Override
    public void doChain() {
        for (ApplicationListener<ApplicationCloseEvent> listener : listeners) {
            listener.onApplicationEvent(event);
        }
    }
}
